package oa.hleast.xswl.controller.admin.place;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.enums.QueryModeEnum;
import oa.hleast.common.result.Result;
import oa.hleast.common.web.exception.BizException;
import oa.hleast.xswl.pojo.form.place.CmsPlaceInfoForm;
import oa.hleast.xswl.pojo.form.place.PlaceInfoCommentPageForm;
import oa.hleast.xswl.pojo.form.place.PlaceTypePageForm;
import oa.hleast.xswl.pojo.vo.place.CmsPlaceInfoCommentVo;
import oa.hleast.xswl.pojo.vo.place.CmsPlaceInfoVo;
import oa.hleast.xswl.service.place.IPlaceInfoCommentService;
import oa.hleast.xswl.service.place.IPlaceInfoService;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.List;

/**
 * @author 302
 * @description
 * @date 2021/3/20
 */
@Api(tags = "【系统管理】场所管理")
@RestController("PlaceInfoController")
@RequestMapping("/api.admin/v1/place-info")
@AllArgsConstructor
@Slf4j
public class PlaceInfoController {

    private final IPlaceInfoService placeInfoService;
    private final IPlaceInfoCommentService placeInfoCommentService;

    @ApiOperation(value = "新增场所", httpMethod = "POST")
    @ApiImplicitParam(name = "placeInfoForm", value = "实体JSON对象", required = true, paramType = "body", dataType = "CmsPlaceInfoForm",dataTypeClass = CmsPlaceInfoForm.class)
    @PostMapping("add")
    public Result<Boolean> add(@RequestBody @Valid CmsPlaceInfoForm placeInfoForm) {
        boolean status = placeInfoService.add(placeInfoForm);
        return Result.judge(status);
    }


    @ApiOperation(value = "复制场所", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "placeInfoIds", value = "逗号分隔的场所信息Ids", required = true, paramType = "query", dataType = "String",dataTypeClass = String.class),
            @ApiImplicitParam(name = "newPlaceTypeId", value = "目标场所分类Id", required = true, paramType = "query", dataType = "Long",dataTypeClass = Long.class)
    })
    @PostMapping("copy")
    public Result<Boolean> copy(String placeInfoIds, Long newPlaceTypeId) {
        List<String> ids = Arrays.asList(placeInfoIds.split(","));
        Boolean status = placeInfoService.copy(ids, newPlaceTypeId);
        return Result.judge(status);
    }

    @ApiOperation(value = "修改场所", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "placeInfoForm", value = "实体JSON对象", required = true, paramType = "body", dataType = "CmsPlaceInfoForm",dataTypeClass = CmsPlaceInfoForm.class)
    })
    @PutMapping(value = "update/{id}")
    public Result<Boolean> update(
            @PathVariable @NonNull Integer id,
            @RequestBody @NonNull CmsPlaceInfoForm placeInfoForm) {

        if ((int) id != placeInfoForm.getId()) {
            throw new BizException("数据ID不一致");
        }
        boolean status = placeInfoService.update(placeInfoForm);
        return Result.judge(status);
    }


    @ApiOperation(value = "场所列表分页", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "basePageForm", value = "实体JSON对象", required = true, paramType = "body", dataType = "PlaceTypePageForm",dataTypeClass = PlaceTypePageForm.class)
    })
    @PostMapping("query-list")
    public Result<List<CmsPlaceInfoVo>> list(
            @RequestBody @NonNull PlaceTypePageForm basePageForm) {

        String queryMode = basePageForm.getQueryMode();
        Integer page = basePageForm.getPage();
        Integer limit = basePageForm.getLimit();
        String placeName = basePageForm.getPlaceName();
        String placeTypeId = basePageForm.getPlaceTypeId();
        Integer display = basePageForm.getDisplay();
        log.info("queryMode ={},page ={},limit ={},placeName ={},placeTypeId ={},display ={}", queryMode, page, limit, placeName, placeTypeId, display);

        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        Page<CmsPlaceInfoVo> result = null;
        switch (queryModeEnum) {
            case PAGE:
                result = placeInfoService.pageList(page, limit, display, placeName, placeTypeId);
                return Result.success(result.getRecords(), result.getTotal());
            default:
                throw new BizException("查询模式错误，请联系管理员");
        }
    }

    @ApiOperation(value = "场所详情", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "资讯id", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("get-details/{id}")
    public Result<CmsPlaceInfoVo> detail(@PathVariable Long id) {
        CmsPlaceInfoVo advert = placeInfoService.detail(id);
        return Result.success(advert);
    }

    @ApiOperation(value = "删除场所", httpMethod = "DELETE")
    @ApiImplicitParam(name = "ids", value = "id集合", required = true, paramType = "query", dataType = "String",dataTypeClass = String.class)
    @DeleteMapping("delete/{ids}")
    public Result<Boolean> delete(@PathVariable("ids") @NotBlank String ids) {
        boolean status = placeInfoService.deleteByIds(Arrays.asList(ids.split(",")));
        return Result.judge(status);
    }

    @ApiOperation(value = "场所的评论列表分页", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "basePageForm", value = "实体JSON对象", required = true, paramType = "body", dataType = "PlaceTypePageForm",dataTypeClass = PlaceTypePageForm.class)
    })
    @PostMapping("query-comment-list")
    public Result<List<CmsPlaceInfoCommentVo>> commentList(
            @RequestBody @NonNull PlaceInfoCommentPageForm basePageForm) {

        String queryMode = basePageForm.getQueryMode();
        Integer page = basePageForm.getPage();
        Integer limit = basePageForm.getLimit();
        String placeTypeId = basePageForm.getPlaceInfoId();
        log.info("queryMode ={},page ={},limit ={},placeTypeId ={}", queryMode, page, limit, placeTypeId);

        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        Page<CmsPlaceInfoCommentVo> result = null;
        switch (queryModeEnum) {
            case PAGE:
                result = placeInfoCommentService.pageList(page, limit, placeTypeId);
                return Result.success(result.getRecords(), result.getTotal());
            default:
                throw new BizException("查询模式错误，请联系管理员");
        }
    }

    @ApiOperation(value = "删除评论", httpMethod = "DELETE")
    @ApiImplicitParam(name = "ids", value = "id集合", required = true, paramType = "query", dataType = "String",dataTypeClass = String.class)
    @DeleteMapping("deleteComment/{ids}")
    public Result<Boolean> deleteComment(@PathVariable("ids") @NotBlank String ids) {
        boolean status = placeInfoCommentService.deleteByIds(Arrays.asList(ids.split(",")));
        return Result.judge(status);
    }

}
